import { markRaw } from 'vue'
import { createRouter, createWebHistory, useRoute, createWebHashHistory } from 'vue-router' // createWebHashHistory, createWebHistory
import type { Router, RouteRecordRaw, RouteComponent } from 'vue-router'
import { Help as IconHelp } from '@element-plus/icons-vue'
import {
  House,
  Setting,
  QuestionFilled,
  Operation,
} from '@element-plus/icons-vue'

/* Layout */
const Layout = (): RouteComponent => import('@/layout/index.vue')

import sysNoticeRouter from './modules/systemNotice'
import signConfigRouter from './modules/signConfig'
import cityRouter from './modules/city'
import inviteRouter from './modules/invite'
import statementRouter from './modules/statement'
import achievementRouter from './modules/achievement'
import levelRouter from './modules/level'
import financeRouter from './modules/finance'
import orderRouter from './modules/order'
import playerRouter from './modules/player'
import brokerRouter from './modules/brokeManage'
import userRouter from './modules/user'
import homeRouter from './modules/home'
import systemParamRouter from './modules/systemParam'
console.log('1234', signConfigRouter)
console.log('55555=====', sysNoticeRouter)

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 *
 * 注意：hidden、alwaysShow 属性配置移动到了meta中！！！
 */
export const constantRoutes: RouteRecordRaw[] = [
  {
    path: '/redirect',
    component: Layout,
    meta: { hidden: true },
    children: [
      {
        path: '/redirect/:path(.*)',
        component: () => import('@/views/redirect/index.vue'),
      },
    ],
  },
  {
    path: '/login',
    component: () => import('@/views/login/index.vue'),
    meta: { hidden: true },
  },
  {
    path: '/auth-redirect',
    component: () => import('@/views/login/auth-redirect.vue'),
    meta: { hidden: true },
  },
  {
    path: '/404',
    component: () => import('@/views/error-page/404.vue'),
    meta: { hidden: true },
  },
  {
    path: '/401',
    component: () => import('@/views/error-page/401.vue'),
    meta: { hidden: true },
  },
  {
    path: '/',
    component: Layout,
    name: 'dashboard',
    redirect: '/dashboard',
    children: [
      {
        path: 'dashboard',
        component: () => import('@/views/dashboard/index.vue'),
        name: 'dashboard',
        meta: { title: '主页', icon: House, affix: true },
      },
    ],
  },
  homeRouter,
  userRouter,
  brokerRouter,
  playerRouter,
  orderRouter,
  financeRouter,
  levelRouter,
  achievementRouter,
  statementRouter, //報表管理
  inviteRouter, //邀請獎勵
  cityRouter, //城市管理
  signConfigRouter, //簽到配置
  // 系统消息
  sysNoticeRouter,
  systemParamRouter,
  // 系統參數
  /* {
    path: '/systemparam',
    component: Layout,
    name: 'systemparam',
    meta: { title: '系統參數', icon: Operation, alwaysShow: true },
    // redirect: '/systemparam',
    children: [
      {
        path: 'index',
        component: () => import('@/views/systemParam/index.vue'),
        name: 'index',
        meta: { title: '系統參數', icon: Operation },
      },
      {
        path: 'editParam',
        component: () => import('@/views/systemParam/editParam.vue'),
        name: 'editParam',
        meta: { title: '系统参数编辑', icon: Operation, hidden: true },
      },
    ],
  }, */
  // 帮助与反馈
  {
    path: '/feedback',
    component: Layout,
    children: [
      {
        path: 'feedbackIndex',
        component: () => import('@/views/feedback/index.vue'),
        name: 'feedback',
        meta: { title: '幫助與反饋', icon: QuestionFilled },
      },
    ],
  },
  // 系統
  {
    path: '/system',
    component: Layout,
    meta: { title: '系統', icon: Setting },
    children: [
      {
        path: 'index',
        component: () => import('@/views/system/index.vue'),
        name: 'system',
        meta: { title: '系統', icon: Setting, noCache: true },
      },
      {
        path: 'add',
        component: () => import('@/views/system/add.vue'),
        name: 'add',
        meta: { title: '新增管理員', icon: Setting },
      },
      {
        path: 'edit',
        component: () => import('@/views/system/editAdmin.vue'),
        name: 'editAdmin',
        meta: { title: '編輯管理員', icon: Setting },
      },
      {
        path: 'roleIndex',
        component: () => import('@/views/system/roleIndex.vue'),
        name: 'roleIndex',
        meta: { title: '角色', icon: Setting, noCache: true },
      },
      {
        path: 'addRole',
        component: () => import('@/views/system/addRole.vue'),
        name: 'addRole',
        meta: { title: '新增角色', icon: Setting },
      },
      {
        path: 'editRole',
        component: () => import('@/views/system/editRole.vue'),
        name: 'editRole',
        meta: { title: '編輯角色', icon: Setting },
      },
    ],
  },
  {
    path: '/profile',
    component: Layout,
    redirect: '/profile/index',
    meta: { hidden: true },
    children: [
      {
        path: 'index',
        component: () => import('@/views/profile/index.vue'),
        name: 'Profile',
        meta: { title: '设置', icon: 'user', noCache: true },
      },
    ],
  },
]

/**
 * asyncRoutes
 * the routes that need to be dynamically loaded based on user roles
 *
 * 注意：hidden、alwaysShow 属性配置移动到了meta中！！！
 */
export const asyncRoutes: RouteRecordRaw[] = [
  {
    path: '/error',
    component: Layout,
    redirect: 'noRedirect',
    name: 'ErrorPages',
    meta: {
      title: 'Error Pages',
      icon: '404',
    },
    children: [
      {
        path: '401',
        component: () => import('@/views/error-page/401.vue'),
        name: 'Page401',
        meta: { title: '401', noCache: true },
      },
      {
        path: '404',
        component: () => import('@/views/error-page/404.vue'),
        name: 'Page404',
        meta: { title: '404', noCache: true },
      },
    ],
  },

  // 404 page must be placed at the end !!!
  { path: '/:pathMatch(.*)*', redirect: '/404', meta: { hidden: true } },
]

console.log('BASE_URL=', import.meta.env)

const createTheRouter = (): Router =>
  createRouter({
    history: createWebHashHistory(import.meta.env.BASE_URL),
    // 注意，如果要配置 HTML5 模式，则需要修改nginx配置，参考资料：
    // https://router.vuejs.org/zh/guide/essentials/history-mode.html
    // history: createWebHistory(import.meta.env.BASE_URL),
    scrollBehavior: () => ({ top: 0 }),
    routes: constantRoutes,
  })

interface RouterPro extends Router {
  matcher: unknown
}

// const route = useRoute()
const router = createTheRouter() as RouterPro

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createTheRouter() as RouterPro
  router.matcher = newRouter.matcher // reset router
}

export default router
